package cn.bnuz.edu.liyang.wait_eat_what;

import android.app.Instrumentation;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Drink extends AppCompatActivity {
    ListView drinklist;
    private List<Map<String,Object>> things = new ArrayList<Map<String,Object>>();
    SimpleAdapter adapter;
    ContentValues values;
    DataBase dbHelper = new DataBase(this,"Thing",null,1);
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.drink);

        //返回按钮
        Button backbutton = (Button)findViewById(R.id.backbutton);
        backbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread() {
                    public void run() {
                        try {
                            Instrumentation instrument = new Instrumentation();
                            instrument.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);
                        } catch (Exception e) {
                        }            }        }.start();    }});

        //点击人像按钮弹出登录界面
        ImageButton loginbutton = (ImageButton) findViewById(R.id.loginbuttom_drink);
        loginbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Drink.this,Login.class);
                startActivity(intent);
            }
        });

//        //数据库
//        db = dbHelper.getWritableDatabase();
//        values = new ContentValues();
//        values.put("name","芒果冰冰");
//        values.put("fromm","东北人家");
//        values.put("img_url",R.drawable.xban0);
//        values.put("adress","学三二楼");
//        values.put("price","12.0");
//        values.put("stars","4.9");
//        db.insert("Thing",null,values);
//        db.delete("Thing","name=?",new String[]{"红烧猪脚"});
        drinklist =findViewById(R.id.list_list);
        if(){
            do{
                Map<String,Object> map = new HashMap<String,Object>();
                String name = cursor.getString(cursor.getColumnIndex("name"));
                String from = cursor.getString(cursor.getColumnIndex("fromm"));
                String img_url = cursor.getString(cursor.getColumnIndex("img_url"));
                String adress = cursor.getString(cursor.getColumnIndex("adress"));
                Double price = cursor.getDouble(cursor.getColumnIndex("price"));
                Double stars = cursor.getDouble(cursor.getColumnIndex("stars"));
                map.put("name",name);
                map.put("from",from);
                map.put("img_url",img_url);
                map.put("adress",adress);
                map.put("price",price);
                map.put("stars",stars);
                things.add(map);
            }while(cursor.moveToNext());
        }
        adapter = new SimpleAdapter(this,things,R.layout.list_item,new String[]{"name","from","img_url","price","stars"},new int[]{R.id.thing_name,R.id.thing_from,R.id.thing_image,R.id.thing_price,R.id.thing_stars});
        drinklist.setOnItemClickListener(new AdapterView.OnItemClickListener(){

            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(Drink.this,Detail.class);
                Bundle bundle = new Bundle();
                TextView name = (TextView)view.findViewById(R.id.thing_name);
                TextView fromm = (TextView)view.findViewById(R.id.thing_from);
                String name1 = (String) name.getText();
                String fromm1 = (String) fromm.getText();
                Things detail = (Things)ThingDB.getThingsList(1,0,name1,fromm1,null,true).get(0);
                bundle.putSerializable("detail",detail);
                intent.putExtra("detail",bundle);
                startActivity(intent);
            }
        });
        drinklist.setAdapter(adapter);
    }
}
